package com.lujieni.k8sprovider;

import com.sun.management.OperatingSystemMXBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.lang.management.ManagementFactory;
import java.sql.SQLOutput;
import java.text.NumberFormat;

@SpringBootApplication
@MapperScan("com.lujieni.**.dao")
public class K8sProviderApplication {

    public static void main(String[] args) {

        Runtime runtime = Runtime.getRuntime();
        NumberFormat format = NumberFormat.getInstance();

        long maxMemory = runtime.maxMemory();
        long allocatedMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        long mb = 1024 * 1024;
        String mega = " MB";

        long physicalMemory;

        try {
            physicalMemory = ((OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize();
        }catch (Exception e){
            physicalMemory = -1L;
        }

        int availableCores = Runtime.getRuntime().availableProcessors();

        System.out.println("=====================System Info========================");
        System.out.println("CPU Cores: "+availableCores);
        if(physicalMemory != -1L){
            System.out.println("Physical Memory: "+format.format(physicalMemory / mb) + mega);
        }

        System.out.println("========================JVM Memory Info=======================");
        System.out.println("Max allowed memory: "+format.format(maxMemory / mb) + mega);
        System.out.println("Allocated memory: "+format.format(allocatedMemory / mb) + mega);
        System.out.println("Used memory in allocated: "+format.format((allocatedMemory - freeMemory) / mb) + mega);
        System.out.println("Free memory in allocated: "+format.format(freeMemory / mb) + mega);



        SpringApplication.run(K8sProviderApplication.class, args);
    }

}
